import React from 'react'
// ===================================================================== Temp
import TableModal from '#cpt/temp/TableModal'
// ===================================================================== Declare
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const Input  			= $Lazy.load(()=>import('@antd/form/input'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		pag: {},
		selectedKeys: [],
		forms : [
			{ label: '账期',			name: 'bill_phase_id',		type: 'select', 	loading:true, },
			{ label: '医院名称',			name: 'hosp_id',			type: 'select', 	loading:true, },
		],
		editIndex: ''
	}
	model = {}
	
	open = (param) => {
		this.getSelect()
		this.modal.open()
		if($fn.hasObject(param)){
			const{bill_phase_id, hosp_id} = param
			this.searchFormRef.setValue({bill_phase_id, hosp_id})
			this.model = {bill_phase_id, hosp_id}
		}
		this.setState({editIndex: ''})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this, 'bill/batchUpPriceList', {paging:'get',isIndex:true})
	
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'bill_phase_id', await f.getCache({ api:'billPhaseSelect' } ) ) 
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			this.setState({ forms })
		})
	}
	
	// 表单
	cols = [
		{ title: '医院名称', 	dataIndex: 'hosp_name', 		width:200 },
		{ title: '账期', 		dataIndex: 'summary_sn', 		width:100 },
		{ title: '项目名称',		dataIndex: 'kind_name', 		width:180 },
		{ title: '标准价',		dataIndex: 'sprice', 			width:100 },
		{ title: '结算价',		dataIndex: 'price', 			width:100,	render:(text,{index,sprice})=>
			index === this.state.editIndex ? <Input p='请输入' value={text}
				onChange={value=> {
					const {data} = this.state
					const price = Number(value).toFixed(2)
					data[index].price = price
					data[index].isChanged = true
					this.setState({data})
				} }
			/> : text
		},
	]
	
	render(){
		return TableModal.call(this,{title:'批量修改',labelWidth:'s4',width:1000,height:'70%',type: 1,hasOkDisabled:false, okText:'提交',reset:false,
			close:()=> {
				this.searchFormRef.form.resetFields()
				this.model = {}
			},
			onOk:()=>{
				const { data } = this.state
				let params = []
				data.forEach(v=>{
					if(v.isChanged){
						const { bill_phase_id, hosp_id, kind_id, sprice, price } = v
						params.push({ bill_phase_id, hosp_id, kind_id, sprice, price })
					}
				})
				$http.submit(null, 'bill/batchUpPriceSubmit', { param:{params} }).then(data=>{
					$fn.msg.success('价格修改已经提交，请及时审核')
					this.modal.close()
				})
			},
			table: { isIndex:true,
				onRow: rows=> this.setState({editIndex:rows.index})
			}
		})

	}
}

export default Index